/*
 * Copyright 2019-2025 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * https://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.iiifi.kite.core.spring;

import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

/**
 * spring 工具类
 *
 * @author kite@iiifi.com 花朝
 */
@Component
@SuppressWarnings("unchecked")
public class SpringContextUtils implements ApplicationContextAware {
    @Nullable
    private static ApplicationContext context;
    @Nullable
    private static String applicationName;
    @Nullable
    private static String port;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        SpringContextUtils.context = context;
        SpringContextUtils.applicationName = context.getId();
        SpringContextUtils.port = context.getEnvironment().getProperty("server.port");
    }

    @Nullable
    public static <T> T getBean(Class<T> clazz) {
        if (context == null) {
            return null;
        }
        return context.getBean(clazz);
    }

    @Nullable
    public static <T> T getBean(String beanName) {
        if (context == null) {
            return null;
        }
        return (T) context.getBean(beanName);
    }

    @Nullable
    public static <T> T getBean(String beanName, Class<T> clazz) {
        if (context == null) {
            return null;
        }
        return context.getBean(beanName, clazz);
    }

    @Nullable
    public static <T> ObjectProvider<T> getBeanProvider(Class<T> clazz) {
        if (context == null) {
            return null;
        }
        return context.getBeanProvider(clazz);
    }

    @Nullable
    public static <T> ObjectProvider<T> getBeanProvider(ResolvableType resolvableType) {
        if (context == null) {
            return null;
        }
        return context.getBeanProvider(resolvableType);
    }

    @Nullable
    public static ApplicationContext getContext() {
        return context;
    }

    @Nullable
    public static String getPort() {
        return port;
    }

    @Nullable
    public static String getApplicationName() {
        return applicationName;
    }

    public static void publishEvent(ApplicationEvent event) {
        if (context == null) {
            return;
        }
        context.publishEvent(event);
    }

    /**
     * 获取aop代理对象
     *
     * @return 代理对象
     */
    public static <T> T getCurrentProxy() {
        return (T) AopContext.currentProxy();
    }

}
